﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CLEXA.ClientTools.Collections;
using CLEXA.DataContracts;
using CLEXA.SurfaceClient.Cache;
using CLEXA.SurfaceClient.View;
using CLEXA.SurfaceClient.ViewModel;
using Windows.UI.Xaml.Controls.Primitives;

namespace CLEXA.SurfaceClient.Functions
{
    public class Home : IHome
    {
        private readonly INavigator navigator;
        private readonly UserData user;
        private readonly IDatabaseCache databaseCache;

        public UserData User
        {
            get { return user; }
        } 

        public IReadOnlyObservableDictionary<int, DatabaseData> Databases
        {
            get { return databaseCache.Databases; }
        }

        public Home(INavigator navigator, UserData user, IDatabaseCache databaseCache)
        {
            this.navigator = navigator;
            this.user = user;
            this.databaseCache = databaseCache;
        }

        public void GoToChangePassword()
        {
            navigator.GoToChangePassword(user);
        }

        public void GoToLogin()
        {
            navigator.GoToLogin();
        }

        public void ShowEditDatabase(int databaseId)
        {
            DatabaseEditView view = new DatabaseEditView();
            Popup popup = new Popup();
            popup.Child = view;
            DatabaseEdit facade = new DatabaseEdit(databaseCache, popup);
            view.DataContext = new DatabaseEditVM(facade, databaseId);
            popup.IsOpen = true;
        }

        public void ShowNewDatabase()
        {
            DatabaseNewView view = new DatabaseNewView();
            Popup popup = new Popup();
            popup.Child = view;
            DatabaseNew facade = new DatabaseNew(databaseCache, popup);
            view.DataContext = new DatabaseNewVM(facade, user);
            popup.IsOpen = true;
        }

        public void ShowDeleteDatabase(int databaseId)
        {
            DatabaseDeleteView view = new DatabaseDeleteView();
            Popup popup = new Popup();
            popup.Child = view;
            DatabaseDelete facade = new DatabaseDelete(databaseCache, popup);
            view.DataContext = new DatabaseDeleteVM(facade, databaseId);
            popup.IsOpen = true;
        }

        public void GoToSchema(DatabaseData database)
        {
            navigator.GoToSchema(user, database);
        }
    }
}
